package cn.sxt.yxl.question.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.yxl.common.util.PageInfoUtil;
import cn.sxt.yxl.question.dao.IAAnswerDao;
import cn.sxt.yxl.question.dao.IAColumnsDao;
import cn.sxt.yxl.question.dao.IAItemsDao;
import cn.sxt.yxl.question.dao.IAQuestionDao;
import cn.sxt.yxl.question.pojo.AAnswer;
import cn.sxt.yxl.question.pojo.AColumns;
import cn.sxt.yxl.question.pojo.AItems;
import cn.sxt.yxl.question.pojo.AQuestion;
import cn.sxt.yxl.question.service.IQuestionService;

@Service("questionService")
public class QuestionServiceImpl implements IQuestionService {

	@Resource
	private IAColumnsDao columnsDao;
	
	@Resource
	private IAItemsDao itemsDao;
	
	@Resource
	private IAQuestionDao questionDao;
	
	@Resource
	private IAAnswerDao answerDao;
	@Override
	public AColumns findOneAColumns(Map<String, Object> condMap) {
		return columnsDao.findOne(condMap);
	}

	@Override
	public JSONObject saveOneAcolumns(AColumns columns) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.columnsDao.saveOne(columns);
		if(res > 0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("res", res);
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","save success");
		}else {
			resultJSON.put("id", "1");
			resultJSON.put("info","save failure");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneAcolumns(AColumns columns) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.columnsDao.updateOne(columns);
		if(res > 0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("res", res);
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","update success");
		}else {
			resultJSON.put("id", "1");
			resultJSON.put("info","update failure");
		}
		return resultJSON;
	}

	@Override
	public List<AColumns> findCondListAcolumnsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil != null) {
			Page<AColumns> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AColumns> columnsList = this.columnsDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));

			return columnsList;
		}
		return this.columnsDao.findCondList(condMap);
	}

	@Override
	public JSONObject deleteOneAcolumns(AColumns columns) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.columnsDao.deleteOne(columns);
		if(res > 0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("res", res);
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","delete success");
		}else {
			resultJSON.put("id", "1");
			resultJSON.put("info","delete failure");
		}
		return resultJSON;
	}

	@Override
	public AItems findOneItems(Map<String, Object> condMap) {
		return this.itemsDao.findOne(condMap);
	}

	@Override
	public JSONObject saveOneItems(AItems items) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.itemsDao.saveOne(items);
		if(res > 0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("res", res);
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","save success");
		}else {
			resultJSON.put("id", "1");
			resultJSON.put("info","save failure");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneItems(AItems items) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.itemsDao.updateOne(items);
		if(res > 0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("res", res);
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","update success");
		}else {
			resultJSON.put("id", "1");
			resultJSON.put("info","update failure");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneItems(AItems items) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.itemsDao.deleteOne(items);
		if(res > 0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("res", res);
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","delete success");
		}else {
			resultJSON.put("id", "1");
			resultJSON.put("info","delete failure");
		}
		return resultJSON;
	}

	@Override
	public List<AItems> findCondListItemsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil != null) {
			Page<AItems> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AItems> itemsList = this.itemsDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));

			return itemsList;
		}
		return this.itemsDao.findCondList(condMap);
	}

	@Override
	public AQuestion findOneQuestion(Map<String, Object> condMap) {
		return this.questionDao.findOne(condMap);
	}

	@Override
	public JSONObject saveOneQuestion(AQuestion question) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.questionDao.saveOne(question);
		if(res > 0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("res", res);
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","save success");
		}else {
			resultJSON.put("id", "1");
			resultJSON.put("info","save failure");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneQuestion(AQuestion question) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.questionDao.updateOne(question);
		if(res > 0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("res", res);
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","update success");
		}else {
			resultJSON.put("id", "1");
			resultJSON.put("info","update failure");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneQuestion(AQuestion question) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.questionDao.deleteOne(question);
		if(res > 0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("res", res);
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","delete success");
		}else {
			resultJSON.put("id", "1");
			resultJSON.put("info","delete failure");
		}
		return resultJSON;
	}

	@Override
	public List<AQuestion> findCondListQuestionService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil != null) {
			Page<AQuestion> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AQuestion> questionList = this.questionDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));

			return questionList;
		}
		return this.questionDao.findCondList(condMap);
	}

	@Override
	public AAnswer findOneAnswer(Map<String, Object> condMap) {
		return this.answerDao.findOne(condMap);
	}

	@Override
	public JSONObject saveOneAnswer(AAnswer answer) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.answerDao.saveOne(answer);
		if(res > 0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("res", res);
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","save success");
		}else {
			resultJSON.put("id", "1");
			resultJSON.put("info","save failure");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneAnswer(AAnswer answer) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.answerDao.updateOne(answer);
		if(res > 0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("res", res);
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","update success");
		}else {
			resultJSON.put("id", "1");
			resultJSON.put("info","update failure");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneAnswer(AAnswer answer) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.answerDao.deleteOne(answer);
		if(res > 0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("res", res);
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","delete success");
		}else {
			resultJSON.put("id", "1");
			resultJSON.put("info","delete failure");
		}
		return resultJSON;
	}

	@Override
	public List<AAnswer> findCondListAnswerService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil != null) {
			Page<AAnswer> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AAnswer> answerList = this.answerDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));

			return answerList;
		}
		return this.answerDao.findCondList(condMap);
	}

}
